package com.punai.standard.service.impl;

import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.punai.common.exception.ServiceException;
import com.punai.common.utils.SecurityUtils;
import com.punai.common.utils.StringUtils;
import com.punai.standard.domain.StaGxFeatureConsume;
import com.punai.standard.domain.vo.StaGxFeatureConsumeVo;
import com.punai.standard.domain.vo.StaSimplifyVo;
import com.punai.standard.mapper.StaGxFeatureConsumeMapper;
import com.punai.standard.service.IStaGxFeatureConsumeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 标准费用项特征描述设置耗量Service业务层处理
 *
 * @author zhx
 * @date 2023-05-15
 */
@Service
public class StaGxFeatureConsumeServiceImpl extends ServiceImpl<StaGxFeatureConsumeMapper,StaGxFeatureConsume> implements IStaGxFeatureConsumeService
{
    @Autowired
    private StaGxFeatureConsumeMapper staGxFeatureConsumeMapper;

    @Override
    public List<StaGxFeatureConsumeVo> getFeatureDescribe(StaGxFeatureConsume staGxFeatureConsume) {String gxId = staGxFeatureConsume.getGxId();
        String versionId = staGxFeatureConsume.getVersionId();
        String address = staGxFeatureConsume.getAddress();

        if(StringUtils.isEmpty(gxId)){
            throw new ServiceException("标准费用项id获取失败");
        }
        if(StringUtils.isEmpty(address)){
            throw new ServiceException("地区获取失败");
        }
        if(StringUtils.isEmpty(versionId)){
            throw new ServiceException("版本获取失败");
        }
        List<StaGxFeatureConsumeVo> voList = new ArrayList<>();

        List<StaSimplifyVo> simplifyVos = staGxFeatureConsumeMapper.selectFeatureIdNameList(gxId, versionId, SecurityUtils.getCompanyId());
        Map<String, String> featureIdNameMap = simplifyVos.stream().collect(Collectors.toMap(StaSimplifyVo::getId, StaSimplifyVo::getName));

        List<StaGxFeatureConsume> staGxFeatureConsumes = staGxFeatureConsumeMapper.selectFeatureDescribe(gxId, versionId,address);
        for (StaGxFeatureConsume gxFeatureConsume : staGxFeatureConsumes) {

            StringBuilder featureItem = new StringBuilder(gxFeatureConsume.getFeatureItem());
            Map<String,List<String>> featureItemMap = JSON.parseObject(featureItem.toString(), Map.class);
            featureItem = new StringBuilder();
            List<String> ids = featureItemMap.get("ids");
            List<String> items = featureItemMap.get("items");
            String lastFeatureId = "";
            for (int i = 0; i < ids.size(); i++) {
                String featureId = ids.get(i);
                if(StringUtils.isEmpty(featureId)){
                    continue;
                }
                String item = items.get(i);
                if(StringUtils.equals(lastFeatureId,featureId)){
                    featureItem.append("|").append(item);
                    continue;
                }
                String featureName;
                if(featureIdNameMap.containsKey(featureId)){
                    featureName = featureIdNameMap.get(featureId);
                }else {
                    featureName = "id:" + featureId + "被特征字典删除";
                }
                featureItem.append(",").append(featureName).append(":").append(item);
                lastFeatureId = featureId;
            }
            StaGxFeatureConsumeVo vo = new StaGxFeatureConsumeVo();
            vo.setId(gxFeatureConsume.getId());
            vo.setGxId(gxId);
            vo.setName(featureItem.substring(1));
            voList.add(vo);
        }

        return voList;
    }
}
